//
//  CommunityViewController.m
//  Four_Fit
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "CommunityViewController.h"
#import <UIButton+WebCache.h>
#import "CommunityCell.h"
#import "Community.h"
#import "CommentViewController.h"

@interface CommunityViewController ()<UITableViewDelegate,UITableViewDataSource>
/** 暂存请求数据所需的参数tokenid */
@property (nonatomic, strong) NSString *tokenid;
/** 轮播图和3个button背后的View */
@property (nonatomic, strong) UIView *backgroundView;
/** 存放scrollView图片 */
@property (nonatomic, strong) NSMutableArray *scrollArray;
/** <#注释#> */
@property (nonatomic, strong) UITableView *tableView;
/** 存放button图片 */
@property (nonatomic, strong) NSMutableArray *buttonArray;
/** 存放button文字 */
@property (nonatomic, strong) NSMutableArray *labelArray;
/** 存放cell上面的模型数据 */
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
/** <#注释#> */
@property (nonatomic, assign) NSInteger page;
@end

static NSString *const cellID = @"community_cell";
@implementation CommunityViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 108)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)scrollArray {
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

- (NSMutableArray *)labelArray {
    if (!_labelArray) {
        _labelArray = [NSMutableArray array];
    }
    return _labelArray;
}

- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 305)];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 5)];
        view.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
        [_backgroundView addSubview:view];
        [self.view addSubview:_backgroundView];
    }
    return _backgroundView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *nowString = [fmt stringFromDate:[NSDate date]];
    NSLog(@"%@",nowString);
    
    self.navigationItem.title = @"社区";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CommunityCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.tableHeaderView = self.backgroundView;
    
    
    [self requestTokenid];
    [self setUpRefresh];
    [self setUpScrollView];
}

- (void)setUpRefresh {
    
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        [self.tableView.mj_footer endRefreshing];
        [self.dataSourceArray removeAllObjects];
        self.page = 1;
        
        //请求tableView的数据
        [self loadTableViewDataWithPage:self.page];
        
        self.tableView.mj_footer.hidden = YES;
    }];
    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.page += 1;
        [self loadTableViewDataWithPage:self.page];
        
    }];
    
}

- (void)requestTokenid {
    
    [[QZRequestManager sharedManager]fetchDataWithUrl:@"http://ws.sythealth.com/ws/common/user/quicklogin?desc=IOS&mobileid=cac6e00a37a1d0549b10a69a5501fff2b38d11c5&product=fit&tokenid=tokenid_undefined&usersid=5784da4c4925c70c581758a9" parameters:@{} method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
       
        self.tokenid = dic[@"data"][@"tokenid"];
        
        [self loadData];
        
    } updateUI:^{
        
    }];
    
    
}

- (void)loadData {
    
    
    
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"tokenid"] = self.tokenid;
    parame[@"userid"] = @"5784da4c4925c70c581758a9";
    
    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://ws.sythealth.com/ws/fit/v53/square/getindex" parameters:parame method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *srollViewDic in dic[@"data"][@"recommendDtoList"]) {
            [self.scrollArray addObject:srollViewDic[@"pic"]];
        }
        for (NSDictionary *btnDic in dic[@"data"][@"indexThemeDtoList"]) {
            [self.buttonArray addObject:btnDic[@"icon"]];
            [self.labelArray addObject:btnDic[@"name"]];
        }
        
       
        
        
    } updateUI:^{
        
        [self setUpButtonWithImage:self.buttonArray label:self.labelArray];
        
        QZSliderView *scrollView = [[QZSliderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) AndImageArray:self.scrollArray];
        [self.backgroundView addSubview:scrollView];
        
    }];
    
}
- (void)loadTableViewDataWithPage:(NSInteger)page {
    NSLog(@"%ld",self.page);
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"tokenid"] = @"tokenid_undefined";
    parame[@"userid"] = @"5784da4c4925c70c581758a9";
    parame[@"page"] = @(page);
    
    [[QZRequestManager sharedManager] fetchDataWithUrl:@"http://ws.sythealth.com/ws/fit/v53/square/recommendlistv531" parameters:parame method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *smallDic in dic[@"data"][@"feedList"]) {
            
            Community *community = [[Community alloc] init];
            [community setValuesForKeysWithDictionary:smallDic];
            [self.dataSourceArray addObject:community];
            
        }
        
    } updateUI:^{
       
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        [self.tableView reloadData];
    }];
    
    
}
//设置轮播图
- (void)setUpScrollView {
    
    
}
//设置button
- (void)setUpButtonWithImage:(NSArray *)imageArray label:(NSArray *)labelArray{
    
    QZSliderView *scrollView = [[QZSliderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) AndImageArray:self.scrollArray];
    [self.backgroundView addSubview:scrollView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 100)];
    view.backgroundColor = [UIColor whiteColor];
    [self.backgroundView addSubview:view];
    
    CGFloat btnW = 65;
    CGFloat Margin = (self.view.frame.size.width - btnW * 3) / 4;
    for (int i = 0; i < 3; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * btnW + Margin * (i + 1), 10, btnW, btnW);
        btn.layer.cornerRadius = btn.frame.size.width * 0.5;
        btn.layer.masksToBounds = YES;
        
        [btn sd_setImageWithURL:[NSURL URLWithString:imageArray[i]] forState:UIControlStateNormal];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(btn.frame.origin.x, 100 - btn.frame.origin.y - 11, btn.frame.size.width, 15)];
        label.text = labelArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        [label setFont:[UIFont systemFontOfSize:14]];
        [label setTextColor:[UIColor darkGrayColor]];
        
        [view addSubview:btn];
        [view addSubview:label];
        
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommunityCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    cell.community = self.dataSourceArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Community *com = self.dataSourceArray[indexPath.row];
    
    return com.cellHeight;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentViewController *comVc = [[CommentViewController alloc] init];
    
    comVc.community = self.dataSourceArray[indexPath.row];
    comVc.index = indexPath.row;
    comVc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:comVc animated:YES];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
